<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        //原型链方法实现继承
        function Person(name) {
            this.name = name;
        }
        Person.prototype.sayHello = function (){
            alert(this.name);
        }

        function Student(score){
            this.score = score;
        }
        Student.prototype = new Person("lhr");//Student的原型指向Person实例
        //Student原型添加新方法
        Student.prototype.sayScore = function () {
            alert(this.score);
        }

        var stu = new Student(100);
        //stu.sayHello();
        //stu.sayScore();
    </script>


    <script>
        //利用call方法实现继承（网上有一些例子也会讲到用apply，但其实两种方式差不多）

        function Animal(name){
            this.name = name;
            this.sayName = function () {
                alert(this.name);
            }
        }

       /* Animal.prototype.sayName = function () {
            alert(this.name);
        }*/

        //通过call子类调用父类构造函数
        function Dog(name,color) {
            this.color = color;
            Animal.call(this,name);
            this.sayColor  = function () {
                alert(this.color);
            }
        }

        Dog.prototype = new Animal();//没有传参
        Dog.prototype.constructor = Dog;//似乎注释掉掉不影响本案例的执行，可能仅对其他方面造成影响
        /*Dog.prototype.sayColor = function () {
            alert(this.color);
        }*/

        var dog = new Dog("hao","white");
        //dog.sayName();
        //dog.sayColor();
    </script>

    <script>
        //根据自己理解写一个

        function Car(speed) {
            this.speed = speed;
            this.saySpeed = function () {
                alert(this.speed);
            }
        }

        function Bus(speed,color){
            this.color = color;
            //Bus.prototype = new Car(speed);//发现这样写继承不到
            Car.call(this,speed);
            this.sayColor = function () {
                alert(color);
            }
        }

        //可能还是得这样写，Bus方法中利用call
        Bus.prototype = new Car();

        var bus = new Bus(100,"red");
        //bus.saySpeed();
        //bus.sayColor();
    </script>

    <script>
        //对象冒充实现继承
        function Parent(val1){
            this.val1 = val1;
            this.sayParent = function () {
                alert(this.val1);
            }
        }

        function Child(val1,val2){
            this.val2 = val2;
            this.child = Parent;
            this.child(val1);
            this.sayChild = function () {
                alert(this.val2);
            }
        }

        var child = new Child(1,2);
        child.sayParent();
        child.sayChild();
    </script>
</head>
<body>

</body>
</html>